package com.whut.work.comment.service.impl;

import com.whut.work.base.model.Page;
import com.whut.work.comment.dao.impl.CommentDaoImpl;
import com.whut.work.comment.model.Comment;
import com.whut.work.comment.service.ICommentService;
import com.whut.work.score.dao.impl.ScoreDaoImpl;
import com.whut.work.score.model.Score;
import com.whut.work.user.dao.impl.UserDaoImpl;
import com.whut.work.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zouyao on 2017/3/16.
 */
@Component
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentDaoImpl commentDao;
    @Autowired
    private UserDaoImpl userDao;

    public Map<String, Object> getCommentRecordByUserId(Integer studentId, Integer teacherId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Comment comment = commentDao.findOne("from Comment c where c.student.id='"+studentId+"' and c.teacherId='"+teacherId+"' ");
        if(comment != null){
            returnMap.put("value", comment);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "无记录");
            returnMap.put("success", false);
        }

        return returnMap;
    }

    public Map<String, Object> setCommentForTeacher(String comment, Integer studentId, Integer teacherId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Comment commentTemp = commentDao.findOne(" from Comment c where c.student.id='"+studentId+"' and c.teacherId='"+teacherId+"' ");

        if(commentTemp != null){//已经评论过
            commentTemp.setComment(comment);//覆盖更新
            commentTemp.setCreateTime(new Date());
            User student = userDao.findOne(" from User u where u.id='"+studentId+"' ");
            commentTemp.setStudent(student);
            commentTemp.setTeacherId(teacherId);
            commentDao.update(commentTemp);
        }else {//没有评论过
            commentTemp = new Comment();
            commentTemp.setCreateTime(new Date());
            commentTemp.setComment(comment);
            User student = userDao.findOne(" from User u where u.id='"+studentId+"' ");
            commentTemp.setStudent(student);
            commentTemp.setTeacherId(teacherId);
            commentDao.save(commentTemp);
        }

        returnMap.put("value", commentTemp);
        returnMap.put("message", "评论成功");
        returnMap.put("success", true);

        return returnMap;
    }

    public Page<Comment> getCommentListOfOneTeacher(Integer currentPage, Integer pageSize,Integer id) throws Exception {
        String queryHql = " from Comment c where c.teacherId='"+id+"' and c.student.id!='"+id+"' ";
        String countHql = "select count(*) from Comment c where c.teacherId='"+id+"' and c.student.id!='"+id+"' ";
        Page<Comment> returnPage = commentDao.findPage(currentPage, pageSize, queryHql, countHql);

        return returnPage;
    }


}
